package com.servlet;

import com.beans.OrderGoods;
import com.beans.OrderInfo;
import com.beans.PageInfo;
import com.dao.OrderDao;
import com.enums.Enums;
import com.util.PageUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OrderServlet.do", value = "/OrderServlet.do")
public class OrderServlet extends HttpServlet {
    OrderDao orderDao = new OrderDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");
        if ("manage".equals(flag)){
            manage(request,response);
        }else if ("updateState".equals(flag)){
            updateState(request,response);
        }else if ("toOrderDetails".equals(flag)){
            toOrderDetails(request,response);
        }
    }

    //跳转到订单信息界面
    private void toOrderDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        OrderInfo orderInfo = orderDao.getOrderDetailById(id);
        List<OrderGoods> orderGoods = orderDao.getGoodsList(id);
        request.setAttribute("orderInfo",orderInfo);
        request.setAttribute("orderGoods",orderGoods);
        request.getRequestDispatcher("/order/order_details.jsp").forward(request,response);
    }

    //更改订单状态为已发货
    private void updateState(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        int result = orderDao.updateState(id);
        if (result==1){
            request.setAttribute("msg","订单发货成功");
            this.manage(request,response);
        }else {
            request.setAttribute("msg","<script>alert('出错了，订单发货失败')</script>");
            this.manage(request,response);
        }
    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderNo = request.getParameter("orderNo");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String orderState = request.getParameter("orderState");
        System.out.println(orderNo);
        System.out.println(beginDate);
        System.out.println(endDate);
        System.out.println(orderState);
        int pageSize = 5;
        int rowCount = orderDao.getOrderCount(orderNo,beginDate,endDate,orderState);
        int pageIndex = 1;
        String pageIndexstr = request.getParameter("pageIndex");
        if(pageIndexstr !=null&&pageIndexstr!="") {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            if(pageIndex>((rowCount+pageSize-1)/pageSize)) {
                pageIndex=((rowCount+pageSize-1)/pageSize);
            }
        }

        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);

        List<OrderInfo> orderList = orderDao.getOrderList(orderNo,beginDate,endDate,orderState,page);

        request.setAttribute("orderNo",orderNo);
        request.setAttribute("beginDate",beginDate);
        request.setAttribute("endDate",endDate);
        request.setAttribute("orderStateList",Enums.EnumOrderState.values());
        request.setAttribute("orderList",orderList);
        request.setAttribute("page",page);

        request.getRequestDispatcher("/order/order_manage.jsp").forward(request,response);
    }
}
